package com.mingqijia.gassafety.console.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.OrganizationalDepartmentAddRequest;
import com.mingqijia.gassafety.webserver.request.OrganizationalDepartmentCommonRequest;
import com.mingqijia.gassafety.webserver.request.OrganizationalDepartmentEditRequest;
import com.mingqijia.gassafety.webserver.request.OrganizationalDepartmentIdRequest;
import com.mingqijia.gassafety.webserver.request.OrganizationalDepartmentListRequest;
import com.mingqijia.gassafety.webserver.response.OrganizationDepartmentResponse;
import com.mingqijia.gassafety.webserver.response.VccOrganizationDepartmentListResponse;
import com.mingqijia.gassafety.webserver.response.VccOrganizationDepartmentTypeDictResponse;
import com.mingqijia.gassafety.webserver.service.OrganizationalDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: gassafety
 * @description: 组织部门管理
 * @author: zm
 * @create: 2024-07-30 10:02
 **/
@Slf4j
@Api(tags = "组织部门管理")
@RestController(value = "OrganizationalDepartmentController")
@RequestMapping("/business/v1/organizationalDepartment")
public class OrganizationalDepartmentController {


    @Autowired
    OrganizationalDepartmentService organizationalDepartmentService;

    /**
     * 组织部门树形结构
     * @return List
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("组织部门列表")
    public CommonResponse<PageUtil<OrganizationDepartmentResponse>> getTreeList(@RequestBody(required = false) OrganizationalDepartmentListRequest req){
        CommonResponse<List<OrganizationDepartmentResponse>> treeList = organizationalDepartmentService.getTreeList(req);
        if (treeList.getCode() == 0) {
           return CommonResponse.success(PageUtil.init(
                    new Page<>(Constants.LONG_ONE, Constants.LONG_1_KILO, treeList.getData().size()),
                    treeList.getData()
            ));
        }
        return CommonResponse.failure(treeList.getMsg());
    }

    /**
     * 组织部门详情
     * @return detail
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation("组织部门详情")
    public CommonResponse<OrganizationDepartmentResponse> detail(@RequestBody @Valid OrganizationalDepartmentIdRequest req){
        return organizationalDepartmentService.detail(req);
    }

    /**
     * 组织部门详情
     * @return detail
     */
    @RequestMapping(value = "/typeDict", method = RequestMethod.GET)
    @ApiOperation("组织部门类型")
    public CommonResponse<List<VccOrganizationDepartmentTypeDictResponse>> typeDict(@RequestParam(value = "dictCode", required = true) String dictCode){
        return organizationalDepartmentService.typeDict(dictCode);
    }

    /**
     * 组织部门新增
     * @return List
     */
    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    @ApiOperation("父级组织部门列表")
    public CommonResponse<List<VccOrganizationDepartmentListResponse>> selectList(@RequestBody(required = false) OrganizationalDepartmentCommonRequest req){
        log.info("父级组织部门列表selectList.code：{}", req.getCode());
        return organizationalDepartmentService.selectList(req.getCode());
    }
    /**
     * 组织部门新增
     * @return List
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("组织部门新增")
    public CommonResponse<Boolean> add(@RequestBody @Valid OrganizationalDepartmentAddRequest req){
        return organizationalDepartmentService.add(req);
    }

    /**
     * 组织部门编辑
     * @return Boolean
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation("组织部门编辑")
    public CommonResponse<Boolean> edit(@RequestBody @Valid OrganizationalDepartmentEditRequest req){
        return organizationalDepartmentService.edit(req);
    }
}
